#!/usr/bin/perl -w
#
# Copyright(c) Plink Limited 2010. All rights reserved.
#
# $Header:$
#
# $Log:$
# 
# Author:      Jeremy Banks
#
# Description: Check for updates to the RSS feed
#

#----------------------------------------------------------------------
# External Modules
#----------------------------------------------------------------------

package XMLTV::Updater;

use strict;

use File::Copy;
use LWP::Simple;
use XML::Simple;

use XMLTV::Logger;
use XMLTV::Settings;

#---------------------------------------------------------------------------
# Globals
#---------------------------------------------------------------------------

my $logger;

#---------------------------------------------------------------------------
# Check for updates
#---------------------------------------------------------------------------

sub check_for_update
{
   my $rss = XMLTV::Settings::rss_feed();

   $logger ||= XMLTV::Logger->instance();
   $logger->die("No rss feed specified") unless $rss;
   my $rss_doc = XMLin(get $rss);
   $logger->die("Couldn't load rss feed: $rss") unless $rss_doc;

   # A file something like:
   # <rss version="2.0">
   #   <channel>
   #     <title>xmlTVNZ Configuration Feed</title>
   #     <item>
   #       <title>{RevisionNumber}</title>
   #       <link>http://www.feedforall.com/restaurant.htm</link>
   #       <comments>{RequiredVersion}</comments>
   #     </item>
   #   </channel>
   # </rss>

   # TODO: version check

   my $revision = $$rss_doc{channel}{item}{title};
   my $current_revision = _get_current_revision();
   $logger->log("Cached marker file version $revision");
   $logger->log("Current marker file version $current_revision");
   _update_marker_file($current_revision, $$rss_doc{channel}{item}{link})
       if $current_revision < $revision;
}

#----------------------------------------------------------------------
# Get the current revision of the cached marker file
#----------------------------------------------------------------------

sub _get_current_revision
{
   my $marker_file = XMLTV::Settings::marker_file();
   if (-e $marker_file)
   {
      my $marker_doc = XMLin($marker_file);
      if ($marker_doc)
      {
         return $$marker_doc{Revision} || -1;
      }
      else
      {
         $logger->log("Couldn't load cached marker file $marker_file");
      }
   }
   return -1;
}

#----------------------------------------------------------------------
# A new version of the marker file is available
#----------------------------------------------------------------------

sub _update_marker_file
{
   my ($revision, $rss_link) = @_;
   my $marker_file = XMLTV::Settings::marker_file();
   my $back_dir = XMLTV::Settings::backup_dir();

   unless (-d $back_dir)                                    # Make the backup directory
   {
      mkdir $back_dir or $logger->die("Couldn't make backup directory $back_dir: $!");
   }

   if (0 < $revision)                                       # If there is an old version then back it up
   {
      my $new_file = $marker_file;
      $new_file = s/\.xml/-$revision.xml/;
      copy $marker_file, "$back_dir/$new_file" or $logger->die("Couldn't copy $marker_file to $back_dir/$new_file: $!");
   }

   my $new_marker_contents = get $rss_link;
   my $new_marker = XMLin($new_marker_contents);
   my $file_revision = $$new_marker{Revision};
   if ($file_revision < $revision)
   {
      $logger->log("Old configuration downloaded, update failed");
      return;
   }

   open FILE, ">$marker_file" or $logger->die("Couldn't open $marker_file for update: $!");
   print FILE $new_marker_contents;
   close FILE;
   $logger->log("$marker_file successfully updated");
}

1;
